package com.ld.collegetask;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.ld.collegetask.beans.BaseJsonBean;
import com.ld.collegetask.databinding.ActivityOrderMemberReleaseNewAddressBinding;
import com.ld.collegetask.serverInterface.RequestSerives;
import com.ld.collegetask.utils.AppApplication;
import com.ld.collegetask.utils.SharePreference;
import com.ld.collegetask.utils.ToastUtil;

import me.leefeng.promptlibrary.PromptDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class OrderMemberReleaseNewAddressActivity extends BaseActivity{

    private ActivityOrderMemberReleaseNewAddressBinding binding;
    private PromptDialog promptDialog;

    private Integer pageNo = 0;
    private Integer userId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_order_member_release_new_address);
        //设置标题
        setTitleView(this,binding.ilTitle,true,true,false,false);
        setLoadingView(this,binding.ilLoading);
        setTitle("新建地址");

        SharePreference sp = new SharePreference(getBaseContext());
        userId = sp.getUserId();

        binding.orderMemberReleaseNewAddressButton.setOnClickListener(this);
        promptDialog = new PromptDialog(this);
    }

    @Override
    public void loading() {
    }

    private void address_member_savelist(String address) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.address_member_save(userId,address);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    promptDialog.dismiss();
                    Intent it = new Intent(OrderMemberReleaseNewAddressActivity.this, OrderMemberReleaseAddressListActivity.class);
                    setResult(1,it);
                    finish();
                } else {
                    promptDialog.showWarn(base.getMsg());
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                ToastUtil.showLong(OrderMemberReleaseNewAddressActivity.this,"网络错误，请检查网络");
                promptDialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.order_member_release_new_address_button:
                if (binding.orderMemberReleaseNewAddress.getText().toString().equals("")){
                    promptDialog.showWarn("请填写地址");
                    return;
                }
                promptDialog.showLoading("正在提交");
                address_member_savelist(binding.orderMemberReleaseNewAddress.getText().toString());
                break;
        }
    }
}